﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
using NLog;

namespace WOMPS
{
    public class HTMLMaker
    {
        private static Logger logger = LogManager.GetCurrentClassLogger(); 

        public static string MakeEmail(List<Series> sl)
        {
            if (logger.IsInfoEnabled)
            {
                logger.Info("Start MakeEmail");
            }
            if (sl.Count == 0)
            {
                return null;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<html>");
            if (Globals.ServerName != null)
            {
                sb.AppendLine("<head><title>New Releases on " + Globals.ServerName + "</title></head>");
            }
            else
            {
                sb.AppendLine("<head><title>New Releases</title></head>");
            }
            sb.AppendLine("<body style='background-color:black; color:#fff'>");
            sb.AppendLine("<div width='500px'>");
            sb.AppendLine("<div style='float:right;  margin-top:-30px;'>");
            if (!Globals.XBMC)
            {
                sb.AppendLine("<img src='https://forums.plex.tv/public/style_images/plex/logo.png' alt='Plex' border='0'>");
            }
            else
            {
                sb.AppendLine("<img src='http://xbmc.org/wp-content/themes/paradise/Paradise/images/logo.png' alt='XBMC' border='0'>");
            }
            sb.AppendLine("</div>");
            sb.AppendLine("<h1 style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif;'>" + Globals.ServerName + "\'s New Releases</h1>");
            sb.AppendLine("<h4 style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif;color:#FF9E01; margin-left:25px; margin-top:-25px;'><i>for " + DateTime.Now.ToLongDateString() +"</i></h4>");
            sb.AppendLine("<!-- start each show --><br /><div id='newitems' style='width:65%'>");
            foreach (Series s in sl)
            {
                sb.AppendLine("<hr style='color: #666;background-color: #666;height: 2px; width:90%'/>");
                if (s.Episodes.Count == 0)
                {
                    //no episodes, just show series information
                    sb.AppendLine("<h2 style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif;'>" + s.SeriesName +" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<small>(" + s.EpisodeCounter + ")</small></h2>");
                    sb.AppendLine("<div style='margin-left:20px; min-height:210px;'>");
                    sb.AppendLine("<div style='margin-left:10px'><div id='showinfo'><div id='pic' style='position: relative; width: 100%;'>");
                    if (s.Overview != "Not found on TVDB.com")
                    {
                        sb.AppendLine("<img src='http://thetvdb.com/banners/posters/" + s.SeriesId + "-1.jpg' alt='" + s.SeriesName + "' border='0' align='center' width='250' height='200'>");
                    }
                    else
                    {
                        if (Globals.XBMC)
                        {
                            sb.AppendLine("<img src='http://xbmc.org/wp-content/themes/paradise/Paradise/images/logo.png' alt='" + s.SeriesName + "' border='0' align='center' width='250' height='200'>");
                        }
                        else
                        {
                            sb.AppendLine("<img src='https://forums.plex.tv/public/style_images/plex/logo.png' alt='" + s.SeriesName + "' border='0' align='center' width='250' height='200'>");
                        }
                    }
                    sb.AppendLine("<h2 style='position: absolute; top: 145px; left: 0; width: 100%;'><span style='color: white;font: bold 12px/25px Helvetica, Sans-Serif;letter-spacing: 1px; background: rgb(0, 0, 0); background: rgba(0, 0, 0, 0.7); padding: 10px;'>" + s.SeriesName + "</span></h2>");
                    sb.AppendLine("</div><br /><div id='showdesc'>");
                    sb.AppendLine("<p style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif; color:#FF9E01'><b>First Aired</b> : " + s.FirstAired.ToShortDateString() + " on " + s.Network);
                    if (Globals.ShowExtendedProp)
                    {
                        sb.AppendLine("<br /><b>File Type</b> : " + s.FileType + "<br /><b>Size on Disk</b> : " + Math.Round(s.FileSize, 3) + " megabytes<br /><b>File Last Modified</b> : " + s.LastModified);
                    }
                    sb.AppendLine("</p><p style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif; color:#FF9E01;'><b>Overview</b> - " + s.Overview + "</p>");
                    sb.AppendLine("</div></div></div>");
                    sb.AppendLine("</div>");
                }
                else{
                   
                    sb.AppendLine("<h2 style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif;'>" + s.SeriesName +" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<small>(" + s.EpisodeCounter + ")</small></h2>");
                    int epCounter = 0;
                    foreach(Episode e in s.Episodes)
                    {   
                        sb.AppendLine("<div style='margin-left:20px; min-height:210px;'>");
                        sb.AppendLine("<div style='margin-left:10px'><div id='showinfo'><div id='pic' style='position: relative; width: 100%;'>");
                        if (e.FileName != "")
                        {
                            sb.AppendLine("<img src='http://thetvdb.com/banners/" + e.FileName + "' alt='" + s.SeriesName + " - " + e.EpisodeName + "' border='0' align='center' width='250' height='200'>");
                        }
                        else 
                        {
                            //use series pic
                            sb.AppendLine("<img src='http://thetvdb.com/banners/posters/" + s.SeriesId + "-1.jpg' alt='" + s.SeriesName + " - " + e.EpisodeName + "' border='0' align='center' width='250' height='200'>");
                        }
                        sb.AppendLine("<h2 style='position: absolute; top: 145px; left: 0px; width: 100%;'><span style='color: white;font: bold 12px/25px Helvetica, Sans-Serif;letter-spacing: 1px; background: rgb(0, 0, 0); background: rgba(0, 0, 0, 0.7); padding: 10px;'>" + e.EpisodeName + "</span></h2>");
						sb.AppendLine("</div><br /><div id='showdesc'>");
                        sb.AppendLine("<p style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif; color:#FF9E01'><b>Season</b> : " + e.SeasonNumber + "<br /><b>Episode</b> : " + e.EpisodeNumber + "<br /><b>First Aired</b> : " + e.FirstAired.ToShortDateString() + " on " + s.Network);
                        if (Globals.ShowExtendedProp)
                        {
                            sb.AppendLine("<br /><b>File Type</b> : " + e.FileType + "<br /><b>Size on Disk</b> : " + Math.Round(e.FileSize, 3) + " megabytes<br /><b>File Last Modified</b> : " + e.LastModified);
                        }
                        sb.AppendLine("</p><p style='font-family: Calibri, Candara, Segoe, Optima, Arial, sans-serif; color:#FF9E01;'><b>Overview</b> - " + e.OverView + "</p>");
						sb.AppendLine("</div></div></div></div>");
                        epCounter++;
                        if (epCounter != s.Episodes.Count())
                        {
                            sb.AppendLine("<hr style='color: #FF9E01;background-color: #FF9E01;height: 1px;width: 35%;'/>");
                        }
                    }
			        //sb.AppendLine("</div>");
                }
            }
            sb.AppendLine("</div></body></html>");
            if (logger.IsInfoEnabled)
            {
                logger.Info("End MakeEmail");
            }
            return sb.ToString();
        }
    }
}
